package work.xiaohong.saolei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.xiaohong.saolei.dao.SaoleiVersionsMapper;
import work.xiaohong.saolei.pojo.Versions;
import work.xiaohong.saolei.service.SaoleiVersionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaoleiVersionsServiceImpl implements SaoleiVersionsService {

    @Autowired
    private SaoleiVersionsMapper saoleiVersionsMapper;


    /**
     * 条件+分页查询SaoleiVersions
     * @param versions 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Versions> findPage(Versions versions, int page, int size){
        // 构建查询条件
        QueryWrapper<Versions> queryWrapper = new QueryWrapper<Versions>(versions);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return saoleiVersionsMapper.selectPage(pg, queryWrapper);
    }

    /**
     * 分页查询SaoleiVersions
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Versions> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return saoleiVersionsMapper.selectPage(pg, null);
    }

    /**
     * 多条件搜索方法SaoleiVersions
     * @param versions
     * @return
     */
    @Override
    public List<Versions> findList(Versions versions){
        // 构建查询条件
        QueryWrapper<Versions> queryWrapper = new QueryWrapper<Versions>(versions);
        // 查询数据
        return saoleiVersionsMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除SaoleiVersions
     * @param id
     */
    @Override
    public void delete(Long id){
        saoleiVersionsMapper.deleteById(id);
    }

    /**
     * 根据ID修改SaoleiVersions
     * @param versions
     */
    @Override
    public void updateById(Versions versions){
        // 初始化一些不可修改数据
        versions.setGmtModified(null);
        versions.setGmtCreate(null);
        versions.setVersion(null);
        versions.setDeleted(null);
        saoleiVersionsMapper.updateById(versions);
    }

    /**
     * 增加SaoleiVersions
     * @param versions
     */
    @Override
    public void add(Versions versions){
        saoleiVersionsMapper.insert(versions);
    }

    /**
     * 根据ID查询SaoleiVersions
     * @param id
     * @return
     */
    @Override
    public Versions findById(Long id){
        return  saoleiVersionsMapper.selectById(id);
    }

    /**
     * 查询全部SaoleiVersions数据
     * @return
     */
    @Override
    public List<Versions> findAll() {
        return saoleiVersionsMapper.selectList(null);
    }

    /**
     * 获取最新的版本
     * @param name
     * @return
     */
    public Versions getLastVersion(String name){
        // 构建查询条件
        QueryWrapper<Versions> queryWrapper = new QueryWrapper<Versions>();
        queryWrapper.eq("name",name);
        queryWrapper.orderByDesc("gmt_create");
        // 查询数据
        List<Versions> versions = saoleiVersionsMapper.selectList(queryWrapper);
        if (versions != null && versions.size() > 0){
            return versions.get(0);
        }else{
            return null;
        }

    }

}
